//-----------------------------------------------------------------------------
// File: PictureBox.cs
//
// Copyright (c) Dmitry Shuklin. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D = Microsoft.DirectX.Direct3D;

namespace Cerebrum.Direct3D.Forms
{
	/// <summary>
	/// Summary description for PictureBox.
	/// </summary>
	public class PictureBox : Cerebrum.Direct3D.Forms.Control 
	{
		public PictureBox()
		{
			//
			// TODO: Add constructor logic here
			//
			m_Image = null;
		}

		protected bool m_OwnerDraw = false;
		[System.ComponentModel.DefaultValue(false)]
		public bool OwnerDraw
		{
			get
			{
				return m_OwnerDraw;
			}
			set
			{
				m_OwnerDraw = value;
			}
		}

		protected System.Drawing.Image m_Image;
		[System.ComponentModel.DefaultValue(null)]
		public System.Drawing.Image Image
		{
			get
			{
				return m_Image;
			}
			set
			{
				if(m_Image != value)
				{
					if(m_Image!=null)
					{
						Cerebrum.Direct3D.Forms.DeviceControl devctl = this.FindDevice();
						Microsoft.DirectX.Direct3D.Device device = null;
						if(devctl!=null && (device=devctl.Device)!=null)
						{
							Cerebrum.Direct3D.Drawing.Graphics.DeviceObjects.DisposeObject(device, this);
						}
					}
					m_Image = value;
				}
			}
		}


		protected override void OnRenderScene(RenderEventArgs e)
		{
			if(m_Image!=null)
			{
				Cerebrum.Direct3D.Drawing.Graphics _Graphics = Cerebrum.Direct3D.Drawing.Graphics.FromDevice(e.Device);
				
				Texture _ImageTexture = Cerebrum.Direct3D.Drawing.Graphics.DeviceObjects[e.Device, this] as Texture;

				if(_ImageTexture == null)
				{
					_ImageTexture = Texture.FromBitmap(e.Device, new System.Drawing.Bitmap(m_Image), 0, Pool.Managed);
					Cerebrum.Direct3D.Drawing.Graphics.DeviceObjects[e.Device, this] = _ImageTexture;
				}
				
				RectangleF _rect = this.SurfaceRectangle;
				float xpos = 0;
				float ypos = 0;
				float w = 0;
				float h = 0;

				if(m_RenderMode==Cerebrum.Direct3D.Drawing.ImageRenderMode.CenterImage)
				{
					xpos = _rect.X + (float)Math.Floor((_rect.Width - m_Image.Width) / 2);
					ypos = _rect.Y + (float)Math.Floor((_rect.Height - m_Image.Height) / 2);
					w = m_Image.Width;
					h = m_Image.Height;

					_rect.X = xpos;
					_rect.Y = ypos;
					_rect.Width = w;
					_rect.Height = h;
				}

				_Graphics.DrawRectangle(_rect, this.m_Z, _ImageTexture, this.m_ForeColor);

				/*
				m_VertexBuffer = new VertexBuffer(typeof(CustomVertex.TransformedColoredTextured), 6,
					device, Usage.WriteOnly | Usage.Dynamic, 0, Pool.SystemMemory);

				RenderStates renderState = device.RenderState;

				renderState.AlphaBlendEnable = true;
				renderState.SourceBlend = Blend.SourceAlpha;
				renderState.DestinationBlend = Blend.InvSourceAlpha;
				renderState.AlphaTestEnable = true;
				renderState.ReferenceAlpha = 0x08;
				renderState.AlphaFunction = Compare.GreaterEqual;

				TextureState textureState0;

				textureState0 = device.TextureState[0];

				textureState0.ColorOperation = TextureOperation.Modulate;
				textureState0.ColorArgument1 = TextureArgument.TextureColor;
				textureState0.ColorArgument2 = TextureArgument.Diffuse;
				textureState0.AlphaOperation = TextureOperation.Modulate;
				textureState0.AlphaArgument1 = TextureArgument.TextureColor;
				textureState0.AlphaArgument2 = TextureArgument.Diffuse;
				textureState0.TextureCoordinateIndex = 0;
				textureState0.TextureTransform = TextureTransform.Disable; // REVIEW

				if(m_ImageTexture ==  null)
				{
					m_ImageTexture = Texture.FromBitmap(device, new System.Drawing.Bitmap(m_Image), 0, Pool.Managed);
				}

				device.SetTexture(0, m_ImageTexture);
				device.VertexFormat = CustomVertex.TransformedColoredTextured.Format;
				device.PixelShader = null;
				device.SetStreamSource(0, m_VertexBuffer, 0);

				CustomVertex.TransformedColoredTextured[] fontVertices = new CustomVertex.TransformedColoredTextured[6];

				RectangleF _rect = this.SurfaceRectangle;
				float xpos = 0;
				float ypos = 0;
				float w = 0;
				float h = 0;

				if(m_RenderMode==Cerebrum.Direct3D.Drawing.ImageRenderMode.StretchImage)
				{
					xpos = _rect.X;
					ypos = _rect.Y;
					w = _rect.Width;
					h = _rect.Height;
				}
				else
				{
					xpos = _rect.X + (_rect.Width - m_Image.Width) / 2;
					ypos = _rect.Y + (_rect.Height - m_Image.Height) / 2;
					w = m_Image.Width;
					h = m_Image.Height;
				}

				float tx1 = 0;
				float ty1 = 0;
				float tx2 = 1.0f;
				float ty2 = 1.0f;

				int color = m_ForeColor.ToArgb();

				fontVertices[0] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+0-0.5f,ypos+h-0.5f,m_Z,1.0f), color, tx1, ty2);
				fontVertices[1] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+0-0.5f,ypos+0-0.5f,m_Z,1.0f), color, tx1, ty1);
				fontVertices[2] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+w-0.5f,ypos+h-0.5f,m_Z,1.0f), color, tx2, ty2);
				fontVertices[3] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+w-0.5f,ypos+0-0.5f,m_Z,1.0f), color, tx2, ty1);
				fontVertices[4] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+w-0.5f,ypos+h-0.5f,m_Z,1.0f), color, tx2, ty2);
				fontVertices[5] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+0-0.5f,ypos+0-0.5f,m_Z,1.0f), color, tx1, ty1);

				m_VertexBuffer.SetData(fontVertices, 0, LockFlags.Discard);
				device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
				device.SetTexture(0, null);

				m_VertexBuffer.Dispose();
				m_VertexBuffer = null;*/
			}
			base.OnRenderScene(e);
		}

		protected Cerebrum.Direct3D.Drawing.ImageRenderMode m_RenderMode = Cerebrum.Direct3D.Drawing.ImageRenderMode.StretchImage;
		[System.ComponentModel.DefaultValue(Cerebrum.Direct3D.Drawing.ImageRenderMode.StretchImage)]
		public Cerebrum.Direct3D.Drawing.ImageRenderMode RenderMode 
		{
			get
			{
				return m_RenderMode;
			}
			set
			{
				m_RenderMode = value;
			}
		}

	}

}
